import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "@/views/index.vue";

import { storage } from "@/utils/storage.js";

const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login.vue"),
  },
  {
    path: "/error",
    name: "error",
    component: () =>
      import(/* webpackChunkName: "error" */ "@/views/error.vue"),
  },
  {
    path: "/",
    name: "layout",
    component: HomeView,

    redirect: "/equipment/index",
    meta: {
      requiresAuth: true, // 在这里设置，表示layout下边的子路由全部需要登录才能访问
    },

    children: [
      {
        path: "/equipment/index",
        name: "equipment",
        component: () => import("@/views/equipment/index.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "equipmentDetail",
        name: "equipmentDetail",
        meta: {
          upper: "equipment",
        },
        component: () => import("@/views/equipment/detail.vue"),
      },
      {
        path: "/mqtt",
        name: "mqttconfig",
        component: () => import("@/views/mqtt/index.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/roles",
        name: "roles",
        component: () => import("@/views/role/list.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/users",
        name: "users",
        component: () => import("@/views/user/list.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/menus",
        name: "menus",
        component: () => import("@/views/menu/list.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/hardware/list",
        name: "hardware",
        component: () => import("@/views/hardware/index.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/productmodel/list",
        name: "productmodel",
        component: () => import("@/views/product/index.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/project/list",
        name: "project",
        component: () => import("@/views/project/index.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/project/edit",
        name: "projectEdit",
        component: () => import("@/views/project/edit.vue"),
      },
      {
        path: "/dept/list",
        name: "dept",
        component: () => import("@/views/dept/index.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/sync/list",
        name: "datamonitoringlist",
        component: () => import("@/views/sync/index.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/sync/details",
        name: "syncDetails",
        meta: {
          upper: "sync",
        },
        component: () => import("@/views/sync/details.vue"),
      },
      {
        path: "/clienttask/list",
        name: "clienttasklist",
        component: () => import("@/views/clientTask/index.vue"),
        meta: { keepAlive: true }
      },
        {
        path: "/clienttask/equipment/list",
        name: "taskEquipmentList",
        component: () => import("@/views/clientTask/equipment/list.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/props/list",
        name: "sysprops",
        component: () => import("@/views/props/index.vue"),
        meta: { keepAlive: true }
      },
       {
        path: "/attach/list",
        name: "attachment",
        component: () => import("@/views/attach/index.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/syslogs/list",
        name: "syslogs",
        component: () => import("@/views/syslogs/index.vue"),
        meta: { keepAlive: true }
      },
      {
        path: "/serial/list",
        name: "serialconfig",
        component: () => import("@/views/serial/index.vue"),
        meta: { keepAlive: true }
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (!to.meta.injected) {
    const map = new Map(flatMenus(storage.get("be-menus") || [], to.name));

    const mergedMeta = {
      ...(to.meta || {}),
      ...(map.get(to.name)?.meta || {}),
    };
    mergedMeta.injected = true;

    to.meta = mergedMeta;
  }

  if (["login", "error"].includes(to.name)) {
    return next();
  }
  
  if (to.meta.requiresAuth) {
    if (storage.get("be-token")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

function flatMenus(menus, code, map = new Map()) {
  menus.forEach((m) => {
    if (m.code && m.code === code) map.set(m.code, m);
    if (Array.isArray(m.sub) && m.sub.length) {
      flatMenus(m.sub, code, map);
    }
  });
  return map;
}

export default router;
